<?php
App::import('Controller', 'Upload');

class UploadXmlController extends UploadController {
    var $helpers = array ('Html','Form','Session');
    var $name = 'Xml'; // riferimento alla cartella delle view
    //var $modelClass = 'Multa';
    
    public function beforeFilter(){
    	
		parent::beforeFilter();
    	$this->loadModel('Parametro');
		
    }

    public function index($attachTipo="XML") {
		// $this->layout="ajax";
		// $this->autorender = false;
    	// App::uses("UploadHandler", "Lib/jqueryupload");
		// $upload_handler = new UploadHandler();
		// die;
		// parent::index($attachTipo);
		// $this->loadModel("Attach");		
		$this->set("attachTipo",$attachTipo);
		$xmls = $this->Xml->find("all",array("conditions" => array("Attach.attach_tipo" => $attachTipo)));
		$this->set("xmls",$xmls);
		
    }
	
	protected function save($type, $file_url, $vars=null) {
    	$fileUploaded = null;
    	try {
	        $fileUploaded = parent::save($type, $file_url, $vars);
            $this->Xml->create();
            $this->Xml->set("stato",XML_NUOVO);
            $this->Xml->set("attach_id",$fileUploaded["id"]);
            $this->Xml->save();
    	} catch (Exception $e1) {
	    	CakeLog::error($e1->getMessage());
			try {
	            $this->Attach->read(null, $fileUploaded["id"]);
				$this->Attach->deleteFile();
			} catch (Exception $e2) {
		    	CakeLog::error($e2->getMessage());
                throw $e2;
			}
			throw $e1;
    	}
		return $fileUploaded;
	}
	
    protected function remove($vars){
        parent::remove($vars);
        $idXml = $vars[1];
        $this->Xml->recursive = -1;
		$this->Xml->read(null,$idXml);
		$this->Xml->delete();
    }

}
?>